package com.chat.controller;

import org.springframework.ai.anthropic.AnthropicChatModel;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.converter.BeanOutputConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @program: springboot3-demos
 * @description:
 * @author: Reagan Li
 * @create: 2024-04-26 10:14
 **/

@RestController
@RequestMapping("/openai")
public class ChatController {

    @Autowired
    private AnthropicChatModel chatModel;

    @GetMapping(value = "/chat")
    public String chat(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
        /*ChatResponse response = chatModel.call(
                new Prompt(
                        "Generate the names of 5 famous pirates.",
                        AnthropicChatOptions.builder()
                                .withModel("claude-2.1")
                                .withTemperature(0.4F)
                                .build()
                ));*/

        String result = chatModel.call(message);
        return result;
    }

    record ActorsFilms(String actor, List<String> movies) {
    }

    record Player(String name, String reasons) {
    }

    @GetMapping(value = "/chat/bean")
    public Player chatBean() {
        /*BeanOutputConverter<ActorsFilms> beanOutputConverter =
                new BeanOutputConverter<>(ActorsFilms.class);

        String format = beanOutputConverter.getFormat();
        String actor = "Tom Hanks";
        String template = """
        Generate the filmography of 5 movies for {actor}.
        {format}
        """;

        Generation generation = chatModel.call(
                new Prompt(new PromptTemplate(template, Map.of("actor", actor, "format", format)).createMessage())).getResult();
        ActorsFilms actorsFilms = beanOutputConverter.convert(generation.getOutput().getContent());
        return actorsFilms;*/

        BeanOutputConverter<Player> beanOutputConverter = new BeanOutputConverter<>(Player.class);
        String format = beanOutputConverter.getFormat();
        String template = """
        Who is the best basketball player
        {format}
        """;

        Generation generation = chatModel.call(
                new Prompt(new PromptTemplate(template, Map.of("format", format)).createMessage())).getResult();
        Player player = beanOutputConverter.convert(generation.getOutput().getContent());
        return player;

    }

}
